package com.appclub.kitsilano.activities;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.MenuItem;
import com.appclub.kitsilano.MainActivity;
import com.appclub.kitsilano.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Activities extends SherlockActivity implements
		ActionBar.TabListener {
	ListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		ActionBar.Tab tab1 = getSupportActionBar().newTab();
		ActionBar.Tab tab2 = getSupportActionBar().newTab();
		tab1.setText("clubs");
		tab2.setText("sport");
		tab1.setTabListener(this);
		tab2.setTabListener(this);
		getSupportActionBar().addTab(tab1);
		getSupportActionBar().addTab(tab2);
		tab1.setTabListener(tabListener);
		tab2.setTabListener(tabListener2);
		setContentView(R.layout.activities_clubs_tab);

		ListView lv = (ListView) findViewById(R.id.club);
		String[] ITEMS = getResources().getStringArray(R.array.Clubs);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, ITEMS);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(Activities.this, Club.class);
				intent.putExtra("n", arg2);
				startActivity(intent);
				overridePendingTransition(R.anim.right_in, R.anim.right_out);
			}
		});
	}

	ActionBar.TabListener tabListener = new ActionBar.TabListener() {
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			setContentView(R.layout.activities_clubs_tab);
			ListView lv = (ListView) findViewById(R.id.club);
			lv.setAdapter(adapter);
			lv.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					Intent intent = new Intent(Activities.this, Club.class);
					intent.putExtra("n", arg2);
					startActivity(intent);
					overridePendingTransition(R.anim.right_in, R.anim.right_out);
				}
			});
		}

		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}

		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}
	};
	ActionBar.TabListener tabListener2 = new ActionBar.TabListener() {
		public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
			setContentView(R.layout.activities_sport_tab);
		}

		public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}

		public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
		}
	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {

	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
		}
		return true;
	}
}